package io.github.xiaomak.gxsdxy_news;

import android.content.Intent;

import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class NewActivity extends AppCompatActivity {
    TextView news;
    AsyncTask asyncTask;
    private SimpleAdapter simpleadapter;
    private List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
    ListView mlistview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        news= (TextView) findViewById(R.id.textView_news);
        Intent intent = getIntent();
        this.setTitle(intent.getStringExtra("title"));
        String url = intent.getStringExtra("url");
        asyncTask=new MyAT().execute(url);
//        mlistview = (ListView) findViewById(R.id.lv);
//        simpleadapter=new SimpleAdapter(this,dataList,R.layout.news_item,new String[]{"imgurl","imgtitle"},new int[]{R.id.nwi,R.id.imgt});
//        mlistview.setAdapter(simpleadapter);


    }

    @Override
    protected void onPause() {
        super.onPause();
        asyncTask.cancel(true);
        asyncTask=null;
    }

    class MyAT extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {
            String url = params[0];
            String con = null;
//            String imgurl;
            try {
                Document doc=Jsoup.connect("http://gxsdxy.cn" + url).get();
                con = doc.select(".field").text();
//                Elements link = doc.select(".field span .colorbox-insert-image img");
//                for(Element data : link){
//                    imgurl=data.attr("src");
//                    Map<String,Object>map=new HashMap<String, Object>();
//                    map.put("imgurl","http://gxsdxy.cn"+imgurl);
//                    map.put("imgtitle","dddddd");
//                    dataList.add(map);
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return con;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            news.setText(s);
        }
    }




}
